import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
import 'package:yw_bapp/dao/merchant_dao.dart';
import 'package:yw_bapp/model/index_data_model.dart';
import 'package:yw_bapp/model/store_qr_code.dart';
import 'package:yw_bapp/provider/store_info_provider.dart';
import 'package:yw_bapp/widgets/big_image_widget.dart';
import 'package:yw_bapp/widgets/custom_route_widget.dart';
import 'package:yw_bapp/widgets/loading_pop_widget.dart';
import 'package:yw_bapp/widgets/m_btn.dart';
import 'package:yw_bapp/widgets/safe_area_bar_widget.dart';
import 'package:yw_bapp/widgets/wrap_gesture_widget.dart';

class QrCode extends StatefulWidget {
  @override
  _QrCodeState createState() => _QrCodeState();
}

class _QrCodeState extends State<QrCode> {
  String qrUrl = '';
  @override
  void initState() {
    // 门店信息
    getStoreQrCode(context);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final qrUrl = context.watch<StoreInfoProvider>().storeInfo.qrCodeUrl ??
        getStoreQrCode(context);
    // 门店信息
    return Scaffold(
      appBar: XAppBar(
        statusBarColor: ColorUtil.color('#FFC49226'),
        bgColor: ColorUtil.color('#FFC49226'),
        title: "我的收款码",
        iconColor: Colors.white,
      ),
      body: Container(
        color: ColorUtil.color("#FFFFFFFF"),
        padding: EdgeInsets.fromLTRB(15, 20, 15, 0),
        child: Container(
          height: 432,
          // color: ColorUtil.color('#FFC49226'),
          padding: EdgeInsets.fromLTRB(17, 13, 17, 20),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(5),
            color: ColorUtil.color('#FFC49226'),
          ),
          child: qrContainer(context),
        ),
      ),
    );
  }

  // 二维码容器
  Widget qrContainer(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          children: [
            Image.asset(
              'images/qr_logo.png',
              width: 36,
              height: 36,
            ),
            Container(
              margin: EdgeInsets.only(left: 3),
              child: Text('二维码收款',
                  style: TextStyle(
                      color: ColorUtil.color("#FFFFFFFF"), fontSize: 18)),
            )
          ],
        ),
        wrapGesture(
          child: Container(
            width: 280,
            height: 280,
            child: Image.network(
              "${context.watch<StoreInfoProvider>().storeInfo.qrCodeUrl ?? getStoreQrCode(context)}",
              fit: BoxFit.cover,
            ),
          ),
          function: () {
            // 点击查看大图
            print("点击查看大图");
            _toViewBigImg(context, qrUrl);
          },
        ),
        //  按钮
        wrapGesture(
          child: Text(
            "保存收款码",
            style:
                TextStyle(color: Colors.white, fontSize: 18, letterSpacing: 2),
          ),
          function: () {
            canvasImg(qrUrl);
          },
        )
      ],
    );
  }

  // 二维码生成
  getStoreQrCode(BuildContext context) async {
    await MerchantDao.GetQrCode().then((StoreQrCodeModel model) =>
        {qrUrl = model.data.qrCodeUrl, print(qrUrl), LoadingPop.hide(context)});
    return qrUrl;
  }

  // 保存图片
  canvasImg(String url) async {
    ByteData bytes = await rootBundle.load('images/noData.png');
    final result = await ImageGallerySaver.saveImage(bytes.buffer.asUint8List());
    print(result);
    // if(result){
    //   print("保存成功");
    // }else{
    //   print("保存失败");
    // }
  }

// 点击查看大图
  Widget _toViewBigImg(BuildContext context, String str) {
    Navigator.of(context).push(CustomRoute(
        BigImageWidget(
          imgUrl: str,
        ),
        2));
  }

  // 动态的生成权限
  Future<bool> requestPermission() async {
    var status = await Permission.photos.status;
    if (status.isUndetermined) {
      Map<Permission, PermissionStatus> statuses = await [
        Permission.photos,
      ].request();
    }
    return status.isGranted;
  }
}
